package top.lanscarlos.ashcraft.controller;

import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.lanscarlos.ashcraft.pojo.Seller;
import top.lanscarlos.ashcraft.pojo.User;
import top.lanscarlos.ashcraft.service.LogService;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft.controller
 *
 * @author Lanscarlos
 * @since 2022-05-25 16:21
 */
@RestController
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class LogController extends AshController {
    @Autowired
    private LogService service;

    @RequestMapping(value = "/login")
    public Object login(JsonObject json, String phone, String password) {
        Object object = service.login(phone, password);
        if (object instanceof User) {
            // 用户
            json.addProperty("result", true);
            json.addProperty("type", "user");
            json.add("user", toJson(object));
        } else if (object instanceof Seller) {
            // 商家
            json.addProperty("result", true);
            json.addProperty("type", "seller");
            json.add("seller", toJson(object));
        } else {
            json.addProperty("result", false);
            json.addProperty("type", "unknown");
        }
        return json;
    }
}
